package com.czk.getApplcationContext;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 第二种方式
 * 实现ApplicationContextAware接口
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/28 15:05
 */
public class ApplicationUtils implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("setApplicationContext被调用了");
        ApplicationUtils.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return ApplicationUtils.applicationContext.getBean(clazz);
    }
}
